package com.leo;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 * @ClassName MouseDemo
 * @Description
 * @Author wangss
 * @date 2020.06.04 22:37
 * @Version 1.0
 */
public class MouseDemo5 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(an);

        HBox hBox = new HBox(20);
        hBox.setStyle("-fx-background-color: #b9ee53");
        hBox.setPrefWidth(400d);
        hBox.setPrefHeight(400d);

        Button b1 = new Button("button1");
        Label label = new Label("hello word");
        label.setFont(new Font(20));
        label.setStyle("-fx-background-color: #eea5c4");

        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().addAll(b1, label);

        AnchorPane.setTopAnchor(hBox, 100d);
        AnchorPane.setLeftAnchor(hBox, 100d);
        an.getChildren().add(hBox);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ImageViewDemo");
        primaryStage.setWidth(600d);
        primaryStage.setHeight(600d);
        primaryStage.setResizable(false);
        primaryStage.show();

        // 这里所有得组件都可以添加
        /*b1.addEventHandler(MouseEvent.ANY, event -> {
            System.out.println("监听鼠标得所有事件");
        });*/

        // filter执行早于handler
        System.out.println("================ filter 事件传递是从父到子 =========================");
        b1.addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
            System.out.println("b1 filter MOUSE_CLICKED");
        });

        hBox.addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
            System.out.println("hBox filter MOUSE_CLICKED");
        });

        an.addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
            System.out.println("an filter MOUSE_CLICKED");
        });

        System.out.println("================ handler 事件传递是从子到父 =========================");
        b1.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
            // button这种方式，本身不会将事件传递，相当于执行了event.consume();
            System.out.println("b1 handler MOUSE_CLICKED");
        });

        hBox.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
            System.out.println("hBox handler MOUSE_CLICKED");
        });

        an.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
            System.out.println("an handler MOUSE_CLICKED");
        });

        label.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
            // 阻止事件传递
            event.consume();
            System.out.println("label handler MOUSE_CLICKED");
        });
    }
}
